package com.ccys.netty.handler;

import cn.hutool.core.io.FileUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.CharSetUtils;
import org.apache.http.util.CharsetUtils;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.List;


/**
 * @description 自定义协议解析器
 * @date     2022/10/19
 * @author   cloud fly
 * @description http://www.zzvips.com/article/173439.html
 * https://blog.csdn.net/hahazz233/article/details/125315569
 * netty 协议  https://blog.csdn.net/sgr011215/article/details/124599539
 * 自定义协议解析https://wenku.baidu.com/view/a25b1ee3b84cf7ec4afe04a1b0717fd5360cb2c1.html
 * https://blog.csdn.net/weixin_41402069/article/details/125716436
 * 自定义解析 https://blog.csdn.net/qq_42986622/article/details/121436794
 *
 * 编码解码解析器https://blog.csdn.net/pengjianglilive/article/details/122476580
 * 接收消息转16进制  http://www.360doc.com/content/22/0610/23/39194723_1035517987.shtml
 */
@Slf4j
public class CustomByteToNbMessageDecoder extends ByteToMessageDecoder {


    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        log.info("===自定义解码器执行===");
        //创建字节数组,buffer.readableBytes可读字节长度
        byte[] b = new byte[byteBuf.readableBytes()];
        FileUtil.writeBytes(b,"H:\\downLoad\\test.txt");
        //复制内容到字节数组b
        byteBuf.readBytes(b);
        //字节数组转字符串
        String str = new String(b);//, CharsetUtil.UTF_8
        System.out.println("自定义解码转字符串后的数据=" + str);
        /**
         *     public static final Charset UTF_16 = Charset.forName("UTF-16");
         *     public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
         *     public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
         *     public static final Charset UTF_8 = Charset.forName("UTF-8");
         *     public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
         *     public static final Charset US_ASCII = Charset.forName("US-ASCII");
         */
        /*String UTF_16 = new String(b, CharsetUtil.UTF_16);
        String UTF_16BE = new String(b, CharsetUtil.UTF_16BE);
        String UTF_16LE = new String(b, CharsetUtil.UTF_16LE);
        String ISO_8859_1 = new String(b, CharsetUtil.ISO_8859_1);
        String US_ASCII = new String(b, CharsetUtil.US_ASCII);
        String UTF_8 = new String(b, CharsetUtil.UTF_8);
        String GBK = new String(b, "gbk");
        String gb18030 = new String(b, "gb18030");

        System.out.println("UTF_16=" + UTF_16);
        System.out.println("UTF_16BE=" + UTF_16BE);
        System.out.println("UTF_16LE=" + UTF_16LE);
        System.out.println("ISO_8859_1=" + ISO_8859_1);
        System.out.println("US_ASCII=" + US_ASCII);
        System.out.println("UTF_8=" + UTF_8);
        System.out.println("GBK=" + GBK);
        System.out.println("gb18030=" + gb18030);
        String hex16 = bytesToHexString(b);
        System.out.println("16进制数据=" + hex16);
        String result = hexToString(hex16);
        System.out.println("16转普通字符串转换结果77777777==== " + result);
        String s = toHexString1(b);
        System.out.println("16进制数据字符串=" + s);
        String decode = URLDecoder.decode(UTF_8, "UTF-8");
        System.out.println("url解码打印=" + decode);*/
        list.add(str);
        /*String byteToHex = CustomByteToNbMessageDecoder.bytesToHexString(b);
//           log.info("转字符串=" + str);
//           log.info("转16进制后字符串="+byteToHex);
        if(str.startsWith("86")) {
            list.add(str);
        }else {
            list.add(byteToHex);
        }*/
    }

    public static String hexToString(String hex) {
        StringBuilder output = new StringBuilder();

        for (int i = 0; i < hex.length(); i += 2) {
            String str = hex.substring(i, i + 2);
            output.append((char) Integer.parseInt(str, 16));
        }

        return output.toString();
    }
    public static String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
    public static String toHexString1(byte[] b) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < b.length; ++i) {
            buffer.append(toHexString1(b[i]));
        }
        return buffer.toString();
    }
    public static String toHexString1(byte b) {
        String s = Integer.toHexString(b & 0xFF);
        if (s.length() == 1) {
            return "0" + s;
        } else {
            return s;
        }
    }


}
